:: examples.btm :: 24-jul-97 :: Copyright 1997, JP Software Inc., All Rights Reserved. :: Published by JP Software Inc., P.O. Box 1470, E. Arlington, MA 02174, :: USA, or http://www.jpsoft.com/ :: This sample batch file is distributed with 4DOS, 4OS2, 4NT, and Take :: Command. To see how it works simply run EXAMPLES from your command :: processor's prompt. :: You are free to extract items from this batch file for your own use with :: our products. However, this file can not be modified and then :: redistributed; it is distributed only by JP Software Inc. *@echo off *setlocal *unalias * *unset * on break goto done on error rem window max do forever cls echos Executing: %_batchname under ver /r text This batch file contains a few basic examples of how the various variable functions can be used in your own code. Please press a number [0-8] corresponding to one of the categories below to see some sample output, or any other key to exit. 0. LIST this batch file 1. System status 2. Drives and devices 3. Files 4. File names 5. Strings and characters 6. Numbers and arithmetic 7. Dates and times 8. Utility endtext inkey /c /p /w60 /x Which? %%s cls iff islabel _%s then gosub _%s echo `` echo `` iff "%s" ne "0" then echos Press "L" to LIST this section, echos any other key to return to the menu inkey /c /p /x %%i if "%i" eq "L" list /t":_%s" %_batchname endiff else leave endiff enddo :done window rest list %_batchname return echo 1.FUNCTIONS: System status echo `` setdos /x-5 echo %%@DOSMEM[[b|k|K|m|M]c] Free DOS memory: %@dosmem[bc] bytes echo %%@EMS[[b|k|K|m|M]c] Free EMS memory: %@ems[bc] bytes echo %%@XMS[[b|k|K|m|M]c] Free XMS memory: %@xms[bc] bytes echo %%@EXTENDED[[b|k|K|m|M]c] Free extended memory: %@extended[bc] bytes setdos /x5 echo `` set jp=foo set /m jp=bar if "%@master[jp]" ne "" echos %%@MASTER[varname] iff "%@master[jp]" eq "%jp" then echo String "%%JP" expands to "%@master[jp]" in a single master/local environment. echo String "%%JP" expands to "%jp" locally (shell #%_shell), but to "%@master[jp]" in the master environment. endiff unset /m jp echo `` echo %%@READSCR[row,col,len] The six characters starting with the ninth echo column of the top row of the screen are echo "%@readscr[0,8,6]" return echo 2.FUNCTIONS: Drives and devices echo `` set i=%_disk echo (using current drive "%i:") echo `` echo %%@DISKTOTAL[d:,M] Total disk space: %@format[12,%@disktotal[%i,M]] MB echo %%@DISKUSED[d:,K] Used disk space: %@format[12,%@diskused[%i,K]] KB echo %%@DISKFREE[d:,b] Free disk space: %@format[12,%@diskfree[%i,b]] bytes echo %%@LABEL[d:] Volume label: %@format[13,"%@label[%i]"] echo %%@CDROM[d:] Drive is %@if[%@cdrom[%i] eq 0,NOT ,]a CD-ROM echo %%@READY[d:] Drive is %@if[%@ready[%i] eq 0,NOT ,]ready echo %%@REMOTE[d:] Drive is %@if[%@remote[%i] eq 0,NOT ,]remote echo %%@REMOVABLE[d:] Drive is %@if[%@removable[%i] eq 0,NOT ,]removable echo %%@FSTYPE[d:] The file system is %@FSTYPE[%i] echo `` echo %%@DEVICE[name] for %i in (NUL: CON: COM1: COM5: CLIP:) ( echo Device %i %@if[%@device[%i] eq 0,NOT ,]found echo %%@LPT[n] Printer port LPT2 is %@if[%@lpt[2] eq 0,NOT ,]ready return echo 3.FUNCTIONS: Files %@if[%_rows lt 26,(screen 1),] echo `` set i=%_batchname echo (using this batch file as "filename") echo `` if x%@descript[%i] eq x describe "%i" "Batch language examples" echo %%@DESCRIPT[filename] %@descript[%i] echo %%@TRUENAME[filename] %@truename["%i"] echo %%@ALTNAME[filename] %@altname["%i"] echos %%@ATTRIB[filename,[nrhsda]] File is %@if[%@attrib["%i",r,p] eq 0,NOT ,]readonly echo (%@attrib["%i"]) echo %%@FILEAGE[filename] %@fileage["%i"] echo %%@FILEDATE[filename] %@filedate["%i"] echo %%@FILETIME[filename] %@filetime["%i"] echo %%@LINES[filename] This file contains %@lines["%i"] lines echo %%@LINE[filename,n] Its sixth line is "%@line["%i",5]" echo and its last line is "%@line["%i",%@lines["%i"]]" echo `` echos %%@FILEOPEN[filename,mode] set jp=%@fileopen[%i,read,b] echo Open file for READ in BINARY mode as handle #%jp echos %%@FILESEEK[n,offset,start] Move pointer 8 bytes from beginning echo (result=%@fileseek[%jp,8,0]) echos Move another 10 bytes echo (result=%@fileseek[%jp,10,1]) echo %%@FILEREAD[n [,length]] Read next 5 bytes: "%@fileread[%jp,5]" echo %%@FILECLOSE[n] Close handle #%jp: result=%@fileclose[%jp] echo `` if %_rows lt 26 ( pause Presss any key for more ... cls echo 3.FUNCTIONS: Files (screen 2) echo `` set i=*.txt set j=%@filesize[%i,b] if %j eq -1 set j=0 echo %%@FILESIZE[%i,b] "%i" files occupy %j bytes echo %%FILES[%i] with %@files[%i] matching file(s) echo in this "%_cwd\" directory. echo `` set i=\* echo %%@FINDFIRST[filename[,-nrhsda]] First directory in the root: "%@findfirst[%i,d]" echo %%@FINDNEXT[filename[,-nrhsda]] Next directory in the root: "%@findnext[%i,d]" echo Next directory in the root: "%@findnext[%i,d]" echo etc. echo %%@FINDCLOSE[filename] Close search handle for "%i" (result=%@findclose[%i]) echo `` set i=%@unique[.] echo %%@UNIQUE[d:\path] Create file with unique name: %@filename["%i"] *del %i return echo 4.FUNCTIONS: File names echo `` set i=%_batchname echo (using this batch file as "filename") echo `` echo %%@EXT[filename] Extension only: %@ext["%i"] echo %%@FULL[filename] Name with path: %@full["%i"] echo %%@FILENAME[filename] Name and extension: %@filename["%i"] echo %%@NAME[filename] Name only: %@name["%i"] echo %%@PATH[filename] Path only: %@path["%i"] echo %%@LFN[filename] Long Name: %@lfn[%i] echo %%@SFN[filename] Short Name: %@sfn[%i] echo `` echo %%@EXPAND[filename,[nrhsda]] The *.TXT files in this directory are: echo %@EXPAND[*.txt,-d] return echo 5.FUNCTIONS: Strings and characters %@if[%_rows lt 26,(screen 1),] echo `` set i=%_dow echo %%@ASCII[c] Numeric ASCII value: "%i" is: echos ASCII %@ascii[%@instr[0,1,%i]] + echos ASCII %@ascii[%@instr[1,1,%i]] + echo ASCII %@ascii[%@instr[2,1,%i]] echos %%@SUBSTR[string,start,length] The middle character of "%i" is echo "%@substr[%i,1,1]" echos %%@REPEAT[c,n] Repeat a character: echo %@repeat[-,3] %@repeat[ ,5]- set i= %i %_day` ` echo %%@TRIM[string] Remove blanks from "%i": "%@trim[%i]" set i=Windows echo %%@INSERT[n,string1,string2] "%i" becomes "%@insert[4,s of sorr,%i]" set i=%@random[65,90] echo %%@CHAR[n] ASCII %i is character "%@char[%i]" echo %%@FORMAT[[-][x][.y],string] `|` `|` echo adjust right: %@format[20,%_date] echo %@format[20,%@eval[355/113]] echo %@format[20.5,%@eval[355/113]] echo adjust left: %@format[-20,%_date] echo %@format[-20,%@eval[355/113]] echo %@format[-20.5,%@eval[355/113]] set i=*o[sw]* echos %%@WILD[str1,str2] Wildcard "%i" DOES echo %@if[%@WILD[%_cwd,%i] eq 0,NOT ,]appear in echo "%_cwd". echo `` if %_rows lt 26 ( pause Presss any key for more ... cls echo 5.FUNCTIONS: Strings and characters (screen 2) echo `` set i=The bug which you would fright me with, I seek. echo (using string "%i", echo from William Shakespeare, The Winter's Tale, III,2) echo `` echo %%@LEN[string] The test string contains %@len[%i] characters. echo %%@LOWER[string] "%@lower[%i]" echo %%@UPPER[string] "%@upper[%i]" echo %%@WORDS[["sep",]string] Number of words: %@words[%i] echo %%@WORD[["sep",]n,string] Third word: "%@word[2,%i]" echos %%@INDEX[string1,string2] The first "u" is character # echo %@index["%i",u] echos %%@INSTR[start,length,string] The "f" is followed by: echo "%@INSTR[%@index["%i",f],15,%i]..." echo %%@LEFT[n,string] The leftmost 7 characters are "%@left[7,%i]" echo %%@RIGHT[n,string] The rightmost 7 characters are "%@right[7,%i]" echo %%@REPLACE[str1,str2,str3] "%@replace[ which you would,s you,%i]" echo %%@STRIP[chars,string] No vowels: "%@strip[aeiuo,%i]" return echo 6.FUNCTIONS: Numbers and arithmetic echo `` set i=%@eval[1234567890 / 100000] echo (using an arbitrary number of %i) echo %%@COMMA[n] Inserts thousands separators in a number: %@comma[%i] echo %%@DEC[%%var] Decremented value of a variable: %@dec[%i] echo %%@INC[%%var] Incremented value of a variable: %@inc[%i] echo `` echo %%@EVAL[expression] Arithmetic calculations: 355 / 113 = %@eval[355 / 113=.5] (rounded) set n=%i echos %%@INT[n] Integer part of a number - enter a number: input /c /x /e /l10 /w30 `` %%n screen %_row, %@eval[%_column - %@len[%n] - 27] iff %@numeric[%n] eq 0 then beep set n=%i endiff echo number %n is %@int[%n] ` ` set i=%_4ver echos %%@NUMERIC[string] Test if a string is numeric - enter string: input /c /x /e /l10 /w30 `` %%i screen %_row, %@eval[%_column - %@len[%i] - 36] echo string "%i" is numeric: %@numeric[%i] (0=No, 1=Yes) echos %%@RANDOM[min,max] Generate a random integer: do 8 times echos %@random[0,7], enddo echo etc. echo %%@CONVERT[in,out,value] Base conversion: echo decimal (10): %n echo hexadecimal (16): %@convert[10,16,%n] echo octal (8): %@convert[10,8,%n] echo binary (2): %@convert[10,2,%n] return echo 7.FUNCTIONS: Dates and times echo `` echo (using the current system date of %_date and time of %_time) echo %%@DATE[mm-dd-yy] Convert date to number of days: %@date[%_date] echo %%@MAKEAGE[date[,time]] Convert date/time to file date/time: %@makeage[%_date[,%_time]] echo %%@TIME[hh:mm:ss] Convert time to number of seconds: %@time[%_time] echo %%@DAY[mm-dd-yy] Day of the month: %@day[%_date] echo %%@DOW[mm-dd-yy] Day of the week: %@dow[%_date] echo %%@DOWI[mm-dd-yy] Numeric day of the week: %@dowi[%_date] echo %%@DOY[mm-dd-yy] Numeric day of the year: %@doy[%_date] echo %%@MONTH[mm-dd-yy] Month of the year: %@month[%_date] echo %%@YEAR[mm-dd-yy] Year number (2 digits): %@year[%_date] echo `` echo (using an arbitrary number of 1234 days) echo %%@MAKEDATE[n] Convert number of days to date: %@makedate[1234] echo `` echo (using an arbitrary number of 1234 seconds) echo %%@MAKETIME[n] Convert number of seconds to time: %@maketime[1234] echo `` echo (combining date/time functions) echo In thirty minutes, the time will be %@maketime[%@eval[%@time[%_time]+1800]] echo In one week, the date will be %@makedate[%@eval[%@date[%_date]+7]] return echo 8.FUNCTIONS: Utility timer on /3 > nul echo `` echo (using a temporary alias for FORMAT) alias format=`beep%+echo Too dangerous! ` echo %%@ALIAS[name] Value of an alias: FORMAT is "%@alias[format]" unalias format echo `` echos %%@IF[condition,true,false] Evaluates a test condition: echo Good %@if[%_hour lt 12,morning,afternoon]! echo `` echo (using this file as input - select a line from the box below) set i=%_batchname echos %%@SELECT[file,t,l,b,r,title] Menu selection: echo You selected "%@word[0,%@select[%i,12,2,17,25, This is a test ]]..." echo `` echo (a TIMER was started when you displayed this page) echo %%@TIMER[n] Elapsed time of specified timer: %@timer[3] echo %%@CLIP[n] The first line of your Windows clipboard is: echo "%@clip[0]" return :: examples.btm